var addcion = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin_id coin-type" lay-filter="aihao"><option value=""></option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>'
//添加货币配置
function addCion() {
	$("#coinForm").append(addcion);
	$("#coinForm .layui-form-item:last-child").find("select.coin-type").html($("#cionHtml").html());
}

//删除货币配置
function delCion($this) {
	$this.parents(".layui-form-item").remove();
}

//根据货币ID匹配货币名称
function cinName(id) {
	var coinName;
	$.each($.parseJSON(coin), function(index, value) {
		if(value.id == id) {
			coinName = value.coin_name
		}
	});
	return coinName
}

//获取当前货币配置
function GainCoin() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=lingshou&config=coin',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index, value) {
				$("#coinForm").append('<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin-type coin_id" lay-filter="aihao"><option value="' + value.coin_id + '">' + cinName(value.coin_id) + '</option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="' + value.config + '" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>');
			})

		}
	});
}


//根据等级ID匹配等级名称
function levelName(id) {
	var levelName;
	$.each($.parseJSON(level), function(index, value) {
		if(value.id == id) {
			levelName = value.level_name
		}
	});
	return levelName
}


//添加邀请人配置
function addLevel() {
	var js=$("#level-list .layui-level-list").length+1;
	$("#level-list").append('<div class="layui-level-list"><blockquote class="layui-elem-quote" style="padding: 10px 15px; border-left: none;">'+js+'级邀请人<a class="layui-btn layui-btn-tr layui-yqr-btn" onclick="delLevel($(this))" href="javascript:;"><i class="layui-icon">&#xe640;</i></a></blockquote><div id="inviterConfig"><form id="inviterForm"></form></div></div>');
	$.each($.parseJSON(level), function(index,value) {
		$("#level-list .layui-level-list:last-child #inviterForm").append('<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label level-bt" data-id="'+value.id+'">'+value.level_name+'</label><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-form-mid">封顶值</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="" autocomplete="off" class="layui-input max"></div></div></div>')
	});
	
	
}

//删除邀请人配置
function delLevel($this) {
	 
    var num=$this.parents(".layui-level-list").nextAll().length, max=$(".layui-level-list").length;
    if(num==max-1){
    	layer.alert('至少要保留一级配置', {
			icon: 5
		})
    }else if(num==0){
       $this.parents(".layui-level-list").remove();
    }else{
        layer.alert('只能删除最后一个', {
			icon: 5
		})
    }
}


//获取当前邀请人配置
function GainLevel() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=lingshou&config=inviter',
		success: function(data) {
			var data = data.config_value;
			$.each(data,function(i,v){
				var js = i+1
				$("#level-list").append('<div class="layui-level-list"><blockquote class="layui-elem-quote" style="padding: 10px 15px; border-left: none;">'+js+'级邀请人<a class="layui-btn layui-btn-tr layui-yqr-btn" onclick="delLevel($(this))" href="javascript:;"><i class="layui-icon">&#xe640;</i></a></blockquote><div id="inviterConfig"><form id="inviterForm"></form></div></div>');
				$.each(v,function(j,k){
					$("#level-list .layui-level-list").find("form#inviterForm").eq(i).append('<div class="layui-form-item"><div class="layui-inline level-bt" data-id="'+k.level_id+'"><label class="layui-form-label">'+levelName(k.level_id)+'</label><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+k.rate+'" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-form-mid">封顶值</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+k.max+'" autocomplete="off" class="layui-input max"></div></div></div>');
				})
			})
			

		}
	});
}




layui.use('layer', function() {
	var $ = layui.jquery,layer = layui.layer;
	var active = {
		//保存货币配置
		SubmitCoin: function(){
			var Array = [];
			$("#coinForm .layui-form-item").each(function(index){
				var coin_id = $(this).find("select.coin_id option:selected").val();
				var config = $(this).find("input.config").val();
				var data = {
					coin_id : coin_id,
					config : config
				};
				Array.push(data);
			});
		   console.log(Array)
			$.ajax({
				type:'post',
				dataType:'json',
				url:SubmitCoinSrc,
				data:{
					token:token,
					bonus_name:"lingshou",
					config_name:"coin",
					data:Array
				},
				success:function(data){
		              var code=data.code;
		              var msg=data.msg;
				      if(code == 1) {
							layer.alert(msg, {
								icon: 1
							}, function() {
								location.reload();
							})
						} else {
							layer.alert(msg, {
								icon: 5
							})
						}
				}
			});
		},
		//保存邀请人配置
		SubmitLevel: function(){
			var Array = [];
			$("#level-list .layui-level-list").each(function(index){
				var item = $(this);
				Array[index] = [];
				item.find(".layui-form-item").each(function(i){
					var level_id = $(this).find(".level-bt").attr("data-id");
					var rate = $(this).find("input.rate").val();
					var max = $(this).find("input.max").val();
					var data = {
						level_id: level_id,
						rate: rate,
						max: max
					};
					Array[index].push(data);
				})
			});
			console.log(Array)
			$.ajax({
				type:'post',
				dataType:'json',
				url:SubmitCoinSrc,
				data:{
					token:token,
					bonus_name:"lingshou",
					config_name:"inviter",
					data:Array
				},
				'success':function(data){
		              var code=data.code;
		              var msg=data.msg;
				      if(code == 1) {
							layer.alert(msg, {
								icon: 1
							}, function() {
								location.reload();
							})
						} else {
							layer.alert(msg, {
								icon: 5
							})
						}
				}
			});
				
			
		
			
			
			
		}
	};
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
	
	
	
})


$(function() {
	cionFun();
	GainCoin();
	GainLevel();
})